package com.tanhua.app.controllerservice;

import cn.hutool.core.bean.BeanUtil;
import com.tanhua.api.db.*;
import com.tanhua.domain.db.*;
import com.tanhua.domain.mongo.SoulLock;
import com.tanhua.domain.vo.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.print.attribute.standard.PageRanges;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Richard Wang
 * @date 2021/9/4 15:21
 */
@Service
public class TestSoulService {

    @Reference
    private SoulQuestionListApi soulQuestionListApi;
    @Reference
    private SoulQuestionOptionApi soulQuestionOptionApi;

    @Reference
    private SoulQuestionApi soulQuestionApi;

    @Reference
    private SoulScoreApi soulScoreApi;

    @Reference
    private SoulLockApi soulLockApi;

    @Reference
    private SoulReportApi soulReportApi;

    @Reference
    private SoulStandardApi soulStandardApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private SoulDimensionsApi soulDimensionsApi;


    /**
     * 问卷及试题列表
     *
     * @return
     */
    public ResponseEntity testSoulQuestionList() {

        //1.查询soulLock表,确定当前用户要测试的三种试卷的解锁状态
        Long userId = UserHolder.getUser().getId();
        SoulLock soulLock = soulLockApi.checkLockState(userId);
        //2.将SoulQuestionList的属性赋值到SoulQuestionListVo,并返回List<SoulQuestionListVo>
        List<SoulQuestionListVo> list = soulQuestionListApi.findSoulQuestion(soulLock);

        List<SoulQuestionListVo> voList = new ArrayList<>();
        //3.从SoulReport中查找是否有
        for (SoulQuestionListVo soulQuestionListVo : list) {
            String reportId = soulReportApi.getReportId(UserHolder.getUser().getId(), soulQuestionListVo.getId());
            soulQuestionListVo.setReportId(reportId);
            voList.add(soulQuestionListVo);
        }

        return ResponseEntity.ok(voList);
    }

    /**
     * 提交试卷
     *
     * @param list
     * @return
     */
    public ResponseEntity submitQuestionList(List list) {
        ArrayList<Answers> answersList = new ArrayList<>();
        for (Object obj : list) {
            Answers answers = new Answers();
            BeanUtil.copyProperties(obj, answers);
            answersList.add(answers);
        }
        //answersList={Answers(questionId=8, optionId=15), Answers(questionId=8, optionId=15)}
        //1.查询tb_soul_question_option获得每一个试题对应的选项(也对应得分),从而计算总分
        Integer score = soulQuestionOptionApi.checkScore(answersList);
        //2.查询 tb_soul_question,判断当前题属于初级,中级还是高级
        //获取提交的试题所属的试卷id(1初级,2中级,3高级)
        Answers answers = answersList.get(0);
        Integer questionId = answers.getQuestionId();
        Integer paperId = soulQuestionApi.findPaperId(questionId);

        //3.将userId,试卷Id(等级id),score插入(第二次则覆盖)到soulScore表中
        Long userId = UserHolder.getUser().getId();
        //判断其是否第一次做题
        Boolean flag = soulScoreApi.isNew(userId, paperId);
        if (flag) {
            //第一次:则在SoulScore表中新添加
            soulScoreApi.insert(userId, paperId, score);
        } else {
            //不是第一次:则在SoulScore表中进行覆盖
            soulScoreApi.update(userId, paperId, score);
        }
        //4.在SoulLock的mongoDb表中进行设置,解锁下一份试卷
        soulLockApi.switchOnNextPage(userId, paperId);

        //5.根据score查询等级表,获取等级id
        Integer standardId = soulStandardApi.findStandardId(score);

        //6.根据当前用户的等级id为其生成报告(新增或更新),并返回报告id
        String reportId = soulReportApi.findReportId(userId, paperId, standardId, score);

        return ResponseEntity.ok(reportId);
    }

    /**
     * 查看报告
     *
     * @param reportId
     * @return
     */
    public ResponseEntity checkReport(Integer reportId) {
        //1.查看当前用户的报告
        SoulReport report = soulReportApi.findReport(reportId);

        //2.查询与当前登录用户性格相似的用户
        ArrayList<UserInfo> userInfoList = new ArrayList<>();
        List<Integer> idList = soulScoreApi.findSimilarId(report.getScore(), report.getPaperId());
        for (Integer id : idList) {
            //跳过所查出来的用户为自己的
            if (id == UserHolder.getUser().getId().intValue()) {
                continue;
            }
            UserInfo userInfo = userInfoApi.findUserInfo(id.longValue());
            SoulInfoVo soulInfoVo = new SoulInfoVo();
            soulInfoVo.setId(userInfo.getId().intValue());
            soulInfoVo.setAvatar(userInfo.getAvatar());
            userInfoList.add(userInfo);
        }

        //3.通过报告中的soulStandId,查询当前用户所属的soulStandard(含性格描述,cover等)
        SoulStandard soulStandard = soulStandardApi.findSoulStandard(report.getStandardId());

        ReportVo reportVo = new ReportVo();
        //4.封装conclusion,cover
        BeanUtil.copyProperties(soulStandard, reportVo);

        //5.封装similarYou
        reportVo.setSimilarYou(userInfoList);

        //6.封装dimensions
        List<SoulDimensions> list = soulDimensionsApi.findDimensions(report.getStandardId());
        ArrayList<SoulDimensionsVo> voList = new ArrayList<>();
        for (SoulDimensions soulDimensions : list) {
            SoulDimensionsVo soulDimensionsVo = new SoulDimensionsVo();
            soulDimensionsVo.setKey(soulDimensions.getKeyStr());
            soulDimensionsVo.setValue(soulDimensions.getValueStr());
            voList.add(soulDimensionsVo);
        }
        reportVo.setDimensions(voList);

        return ResponseEntity.ok(reportVo);
    }
}
